package com.sort;

import com.sun.org.apache.xpath.internal.SourceTree;

import java.util.Arrays;

import static com.sort.Code03_BubbleSort.swap;

/**
 * @Author: codermzy
 * @Date: 2024/02/02/21:18
 * @Description: 插入排序
 */
public class Code04_InsertSort {

    public static void main(String[] args) {
        int[] arr = {5, 4, 6, 3, 2, 1};
        insertSort(arr);

        System.out.println(Arrays.toString(arr));
    }

    private static void insertSort(int[] arr) {
        int N = arr.length;
        // int min = arr[0];
        int count1 = 0;
        int count2 = 0;
        for (int i = 1; i < N; i++) {
            // int newIndex = i;
            // while (newIndex > 0 && arr[newIndex] < arr[newIndex - 1]) {
            //     swap(arr, newIndex, newIndex - 1);
            //     newIndex--;
            // }
            for (int j = i; j > 0 && arr[j] < arr[j - 1]; j--) {
                swap(arr, j, j - 1);
            }
        }
        System.out.println(count1 + "," + count2);
    }
}
